﻿// -----------------------------------------------------------------------
// <copyright file="StronglyTypedSingleRowTarget.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.DataReaderConsumer
{
    using System.Collections.Generic;
    using System.Data;

    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class StronglyTypedSingleRowTarget<TInformationType, TAdoNetHelperClass> : IDataReaderConsumer, ITaskDataResult<TInformationType>
        where TInformationType : Dhgms.DataManager.Model.Info.InfoBase<TInformationType>
        where TAdoNetHelperClass : Dhgms.DataManager.Model.Helper.AdoNet.AdoNetBase<TInformationType>, new()
    {
        /// <summary>
        /// Gets the data.
        /// </summary>
        public TInformationType Data { get; set; }

        /// <summary>Processes data reader.</summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns>The number of records processed</returns>
        public long DataReaderAction(IDataReader dataReader)
        {
            var hasRecord = dataReader.Read();
            this.Data = hasRecord ? new TAdoNetHelperClass().GetStronglyTypedObjectFromDataReaderRow(dataReader) : null;

            return hasRecord ? 1 : 0;
        }
    }
}
